/*
 * InformeDeNotasView.java
 *
 * Created on 27 de diciembre de 2007, 23:40
 */
package jschoolmanagerportable.gui.reports;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import jschoolmanagerportable.model.Alumno;
import jschoolmanagerportable.model.Curso;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author  Administrador
 */
public class InformeDeNotasView extends javax.swing.JInternalFrame {

    private JasperPrint jasperPrint;

    /** Creates new form InformeDeNotasView */
    public InformeDeNotasView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = javax.persistence.Persistence.createEntityManagerFactory("JSchoolManagerPortablePU").createEntityManager();
        cursoQuery = entityManager.createQuery("SELECT c FROM Curso c");
        cursoList = org.jdesktop.observablecollections.ObservableCollections.observableList(cursoQuery.getResultList());
        alumnoQuery = entityManager.createQuery("SELECT a FROM Alumno a");
        alumnoList = org.jdesktop.observablecollections.ObservableCollections.observableList(alumnoQuery.getResultList());
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        comboCursos = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        alumnosCurso = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        alumnosSeleccionados = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        periodosCombo = new javax.swing.JComboBox();

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jschoolmanagerportable.JSchoolManagerPortableApp.class).getContext().getResourceMap(InformeDeNotasView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        comboCursos.setName("comboCursos"); // NOI18N

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cursoList, comboCursos);
        bindingGroup.addBinding(jComboBoxBinding);

        comboCursos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCursosActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        alumnosCurso.setName("alumnosCurso"); // NOI18N

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, alumnoList, alumnosCurso);
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(alumnosCurso);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        alumnosSeleccionados.setName("alumnosSeleccionados"); // NOI18N
        jScrollPane2.setViewportView(alumnosSeleccionados);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        periodosCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        periodosCombo.setName("periodosCombo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(comboCursos, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(periodosCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton3)
                            .addComponent(jButton2)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(221, 221, 221))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(comboCursos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(periodosCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                        .addComponent(jButton3)))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void comboCursosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCursosActionPerformed
        Curso curso = (Curso) comboCursos.getSelectedItem();
        if (curso == null) {
            return;
        }
        DefaultListModel dlm = new DefaultListModel();
        for (Alumno alumno : curso.getAlumnoCollection()) {
            dlm.addElement(alumno);
        }
        alumnosCurso.setModel(dlm);
        periodosCombo.removeAllItems();
        if (curso.getIdcolegio().getPeriodos() == 3) {
            periodosCombo.addItem("Primer Trimestre");
            periodosCombo.addItem("Segundo Trimestre");
            periodosCombo.addItem("Tercer Trimestre");
        }else{
            periodosCombo.addItem("Primer Semestre");
            periodosCombo.addItem("Segundo Semestre");            
        }        
    }//GEN-LAST:event_comboCursosActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        String reportSource = "/reports/templates/InformeDeNotas.jrxml"; //Template
        String reportDest = "/reports/results/InformeDeNotas_" + System.currentTimeMillis();       //Archivo Generado

        File f = new File("");
        String rutaFisica = f.getAbsolutePath();

        Map<String, Object> params = new HashMap<String, Object>();

        int periodo;
        String strPeriodo = (String) periodosCombo.getSelectedItem();
        if(strPeriodo.indexOf("Primer")==0){
            periodo=1;
        }else if(strPeriodo.indexOf("Segundo")==0){
            periodo=2;
        }else{
            periodo=3;
        }
        
        // Select all the items
        int start = 0;
        int end = alumnosSeleccionados.getModel().getSize() - 1;
        if (end >= 0) {
            alumnosSeleccionados.setSelectionInterval(start, end);   // A, B, C, D
        }
        Object[] seleccionados = alumnosSeleccionados.getSelectedValues();
        String condicion = "alumnos.id in (";
        for (Object o : seleccionados) {
            Alumno a = (Alumno) o;
            condicion += String.valueOf(a.getId()) + ",";
        }
        condicion = condicion.substring(0, condicion.length() - 1);
        condicion += ")";
        params.put("alumnos_condicion", condicion);
        params.put("periodo", new Integer(periodo));

        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            Connection con = DriverManager.getConnection("jdbc:derby:jsmpDB", "root", "jug.cl");
            JasperReport jasperReport = JasperCompileManager.compileReport(rutaFisica + reportSource);
            jasperPrint = JasperFillManager.fillReport(jasperReport, params, con);
            con.close();

            JasperViewer jasperviewer = new JasperViewer(jasperPrint, false);
            jasperviewer.setTitle("JSchoolManager *Portable* - Listado de Alumnos");
            jasperviewer.setExtendedState(0);
            jasperviewer.setFocusable(true);
            jasperviewer.setFocusableWindowState(true);
            jasperviewer.setResizable(true);
            jasperviewer.setEnabled(true);
            jasperviewer.setVisible(true);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Object[] seleccionados = alumnosCurso.getSelectedValues();
        DefaultListModel dlm = new DefaultListModel();
        for (Object o : seleccionados) {
            dlm.addElement(o);
        }
        alumnosSeleccionados.setModel(dlm);
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<jschoolmanagerportable.model.Alumno> alumnoList;
    private javax.persistence.Query alumnoQuery;
    private javax.swing.JList alumnosCurso;
    private javax.swing.JList alumnosSeleccionados;
    private javax.swing.JComboBox comboCursos;
    private java.util.List<jschoolmanagerportable.model.Curso> cursoList;
    private javax.persistence.Query cursoQuery;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox periodosCombo;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
